<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        .el-page-header__content {
            color: white;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div id="app">
    <el-page-header style="background-color: springgreen;
            color: white;line-height: 60px" @back="goBack" content="添加案例">
    </el-page-header>
    <el-card style="width: 700px;height: 1700px;margin: 0 auto">
        <el-form label-width="90px">
            <el-form-item label="案例标题">
                <el-input type="text" v-model="c.name"></el-input>
            </el-form-item>
            <el-form-item label="案例面积">
                <el-input type="text" v-model="c.area"></el-input>
            </el-form-item>
            <el-form-item label="案例风格">
                <el-select v-model="c.styleId">
                    <el-option v-for="st in styleArr"
                               :label="st.style" :value="st.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="案例户型">
                <el-select v-model="c.typeId">
                    <el-option v-for="ty in typeArr"
                               :label="ty.type" :value="ty.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="案例封面">
                <el-upload
                        action="/upload"
                        name="pic"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccessCover"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemoveCover">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

            <el-form-item label="客厅图片">
                <el-upload
                        action="/upload"
                        name="pic"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccessParlour"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemoveParlour">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

            <el-form-item label="卧室图片">
                <el-upload
                        action="/upload"
                        name="pic"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccessBedroom"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemoveBedroom">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

            <el-form-item label="餐厅图片">
                <el-upload
                        action="/upload"
                        name="pic"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccessRestaurant"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemoveRestaurant">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

            <el-form-item label="卫生间图片">
                <el-upload
                        action="/upload"
                        name="pic"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccessToilet"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemoveToilet">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

            <el-form-item label="厨房图片">
                <el-upload
                        action="/upload"
                        name="pic"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccessKitchen"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemoveKitchen">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="insert()">添加案例</el-button>
            </el-form-item>
        </el-form>
    </el-card>


</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                c: {
                    name: "",
                    area: "",
                    styleId: "",
                    typeId: "",
                    cover: "",//封面
                    parlourUrl: "",//客厅url
                    bedroomUrl: "",//卧室url
                    restaurantUrl: "",//餐厅url
                    toiletUrl: "",//卫生间url
                    kitchenUrl: ""//厨房url
                },
                styleArr: [],
                typeArr:[]
            }
        },
        methods: {
            insert() {
                if (v.c.cover == "") {
                    v.$message.error("请选择上传的封面!");
                    return;
                }
                if (v.c.parlourUrl == "") {
                    v.$message.error("请选择上传的客厅!");
                    return;
                }
                if (v.c.bedroomUrl == "") {
                    v.$message.error("请选择上传的卧室!");
                    return;
                }
                if (v.c.restaurantUrl == "") {
                    v.$message.error("请选择上传的餐厅!");
                    return;
                }
                if (v.c.toiletUrl == "") {
                    v.$message.error("请选择上传的卫生间!");
                    return;
                }
                if (v.c.kitchenUrl == "") {
                    v.$message.error("请选择上传的厨房!");
                    return;
                }
                //发出添加商品的请求
                axios.post("/case/insert", v.c).then(function (response) {
                    location.href = "/caseadmin.html";
                })

            },
            //封面
            handleSuccessCover(response, file, fileList) {
                //response代表服务器响应的图片路径
                v.c.cover = response;
            },

            handleRemoveCover(file, fileList) {
                console.log(file, fileList);
                //发出删除图片的请求
                axios.get("/remove?url=" + v.c.cover).then(function () {
                    v.$message("服务器图片删除完成");
                    v.c.cover = "";
                })
            },
            //客厅
            handleSuccessParlour(response, file, fileList) {
                //response代表服务器响应的图片路径
                v.c.parlourUrl = response;
            },

            handleRemoveParlour(file, fileList) {
                console.log(file, fileList);
                //发出删除图片的请求
                axios.get("/remove?url=" + v.c.parlourUrl).then(function () {
                    v.$message("服务器图片删除完成");
                    v.c.parlourUrl = "";
                })
            },
            //卧室
            handleSuccessBedroom(response, file, fileList) {
                //response代表服务器响应的图片路径
                v.c.bedroomUrl = response;
            },

            handleRemoveBedroom(file, fileList) {
                console.log(file, fileList);
                //发出删除图片的请求
                axios.get("/remove?url=" + v.c.bedroomUrl).then(function () {
                    v.$message("服务器图片删除完成");
                    v.c.bedroomUrl = "";
                })
            },
            //餐厅
            handleSuccessRestaurant(response, file, fileList) {
                //response代表服务器响应的图片路径
                v.c.restaurantUrl = response;
            },

            handleRemoveRestaurant(file, fileList) {
                console.log(file, fileList);
                //发出删除图片的请求
                axios.get("/remove?url=" + v.c.restaurantUrl).then(function () {
                    v.$message("服务器图片删除完成");
                    v.c.restaurantUrl = "";
                })
            },

            //卫生间
            handleSuccessToilet(response, file, fileList) {
                //response代表服务器响应的图片路径
                v.c.toiletUrl = response;
            },

            handleRemoveToilet(file, fileList) {
                console.log(file, fileList);
                //发出删除图片的请求
                axios.get("/remove?url=" + v.c.toiletUrl).then(function () {
                    v.$message("服务器图片删除完成");
                    v.c.toiletUrl = "";
                })
            },

            //厨房
            handleSuccessKitchen(response, file, fileList) {
                //response代表服务器响应的图片路径
                v.c.kitchenUrl = response;
            },

            handleRemoveKitchen(file, fileList) {
                console.log(file, fileList);
                //发出删除图片的请求
                axios.get("/remove?url=" +v.c.kitchenUrl).then(function () {
                    v.$message("服务器图片删除完成");
                    v.c.kitchenUrl = "";
                })
            },
            //
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            goBack() {
                history.back();//返回上一页面
            }
        },
        created: function () {
            //发请求获取分类数据
            axios.get("/caseType/select").then(function (response) {
                v.typeArr = response.data;
            })
            axios.get("/caseStyle/select").then(function (response) {
                    v.styleArr = response.data;
            })
        }
    })
</script>
</html>